<?php
require_once 'functions.php';
require_once 'config.inc.php';
function getContent($url = '')
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; WOW64; Trident/4.0; SLCC1)"); 
    $content = curl_exec($ch);
    curl_close($ch);
    $encode = mb_detect_encoding($content, array('GB2312','GBK','UTF-8'));
    $content = ($encode == 'UTF-8') ? $content : mb_convert_encoding($content, "UTF-8", "GB2312");
    return $content;
}


if ($_GET['url']) {
    
    $contents = getContent($_GET['url']);
    $contents = strtolower($contents);
    
    $pattern = '/<body>(.+?)<\/body>/is';


    $contents = preg_replace("'<script[^>]*?>.*?</script>'si", "", $contents);      //去掉javascript
    $contents = preg_replace('/<style[^>]*?>(.*?)<\/style>/si', '', $contents);     //style
    $contents = preg_replace('~<([a-z]+?)\s+?.*?>~i','<$1>',$contents);             //去掉所有html标签属性
    $contents = preg_replace("/<\!--.*?-->/si", "", $contents);
    $contents = preg_replace("/\s+/", " ", $contents); //过滤多余回车 

    $datas = explode('<p>', $contents);
    $returnArr['news'] = '';

    foreach ($datas as $value) {
        $end = strpos($value, '</p>');
        if ($end) 
            $row = substr($value, 0, $end);
        else 
            $row = $value;
        
        $row = trim($row);
        if (strpos($row, '<a') === FALSE && strpos($row, '<') === FALSE) {
            $returnArr['news'] .= '<p>'. $row .'</p>';
        }
    }
    $returnArr['error_no'] = 0;
    echo json_encode($returnArr);
    exit(); 


}
$returnArr['error_no'] = 1;
echo json_encode($returnArr);
exit(); 
?>